package com.atguigu.paymentdemo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.paymentdemo.bo.OssUploadResult;
import com.atguigu.paymentdemo.entity.CourseFile;
import com.atguigu.paymentdemo.entity.CourseInfo;
import com.atguigu.paymentdemo.mapper.CourseInfoMapper;
import com.atguigu.paymentdemo.service.CourseFileService;
import com.atguigu.paymentdemo.service.CourseInfoService;
import com.atguigu.paymentdemo.util.AliyunOssUtil;
import com.atguigu.paymentdemo.vo.CourseInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author wulifeng
 */
@Service
@AllArgsConstructor
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, CourseInfo> implements CourseInfoService {
	private final CourseFileService courseFileService;

	@Override
	public List<CourseInfoVo> list(CourseInfo courseInfo) {
		LambdaQueryWrapper<CourseInfo> lqw = Wrappers.lambdaQuery();
		lqw.like(StringUtil.isNotBlank(courseInfo.getCourseName()), CourseInfo::getCourseName, courseInfo.getCourseName());
		return wrappersQuery(lqw);
	}

	@Override
	public List<CourseInfoVo> getByTeacherId(Long teacherId) {
		LambdaQueryWrapper<CourseInfo> lqw = Wrappers.lambdaQuery();
		lqw.eq(CourseInfo::getTeacherId, teacherId);
		return wrappersQuery(lqw);
	}

	List<CourseInfoVo> wrappersQuery(LambdaQueryWrapper<CourseInfo> lqw) {
		List<CourseInfoVo> rsList = new ArrayList<>();
		lqw.orderByDesc(CourseInfo::getPayCount, CourseInfo::getCreateTime);
		List<CourseInfo> infoList = this.baseMapper.selectList(lqw);

		//循环转化为vo对象 并将简介图片第一张设置为课程图片
		for (CourseInfo info : infoList) {
			CourseInfoVo vo = BeanUtil.toBean(info, CourseInfoVo.class);
			Optional<CourseFile> image = courseFileService.getCourseImage(info.getId());
			image.ifPresent(courseFile -> vo.setCourseImage(courseFile.getFileUrl()));

			rsList.add(vo);
		}
		return rsList;
	}

	@Override
	public CourseInfoVo getVoById(Serializable id) {

		CourseInfo info = this.baseMapper.selectById(id);
		CourseInfoVo vo = BeanUtil.toBean(info, CourseInfoVo.class);
		vo.setImageList(courseFileService.getImage(id));
		vo.setVideoList(courseFileService.getVideo(id));
		if (!vo.getImageList().isEmpty()) {
			vo.setCourseImage(vo.getImageList().get(0).getFileUrl());
		}
		return vo;
	}

	@Override
	public void addImage(MultipartFile file, Long id) throws IOException {
		CourseFile add = addFile(file, id);
		add.setFileType("1");
		courseFileService.save(add);

	}

	@Override
	public void addVideo(MultipartFile file, Long id) throws IOException {
		CourseFile add = addFile(file, id);
		add.setFileType("2");
		courseFileService.save(add);
	}

	@Override
	public void delOssFile(Long fileId) {
		courseFileService.removeById(fileId);
	}

	private CourseFile addFile(MultipartFile file, Long id) throws IOException {
		CourseFile add = new CourseFile();
		add.setInfoId(id);
		add.setFileName(file.getName());

		OssUploadResult rs = AliyunOssUtil.upload(file.getBytes());
		add.setFilePath(rs.getFilePath());
		add.setFileUrl(rs.getFileUrl());
		add.setFileSize(rs.getFileSize());
		return add;
	}


	@Override
	public boolean removeById(Serializable id) {
		courseFileService.removeByInfoId(id);
		return super.removeById(id);
	}

	@Override
	public boolean removeByIds(Collection<? extends Serializable> idList) {
		courseFileService.removeByInfoIds(idList);
		return super.removeByIds(idList);
	}
}
